package com.qian.controller;

import com.qian.pojo.Employees;
import com.qian.service.EmployeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tenant")
public class TenantController {

    @Resource
    private EmployeeService employeeService;

    //查询所有租户以及对租户进行模糊查询
    @RequestMapping(value = "/searchByName", method = RequestMethod.POST)
    public ResponseEntity searchAll(@RequestBody Map map) {
        String employeeName = (String) map.get("employeeName");
        List<Employees> list = employeeService.tenantAll(1, employeeName);
        //雪花算法不匹配前端js的最大值，对溢出情况进行处理
        List<String> empId = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            empId.add(list.get(i).getEmployeeId().toString());
        }
        HashMap res = new HashMap();
        res.put("data", list);
        res.put("empId", empId);
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //增加租户
    @RequestMapping(value = "/addTenant", method = RequestMethod.POST)
    public ResponseEntity addTenant(@RequestBody Map map) {
        Employees employees = new Employees();
        employees.setEmployeeAccount((String) map.get("employeeAccount"));
        employees.setEmployeePassword((String) map.get("employeePassword"));
        employees.setEmployeeName((String) map.get("employeeName"));
        employees.setEmployeeGender((String) map.get("employeeGender"));
        employees.setEmployeeCompany((String) map.get("employeeCompany"));
        employees.setEmployeeEmail((String) map.get("employeeEmail"));
        employees.setEmployeeAddress((String) map.get("employeeAddress"));
        employees.setEmployeePosition((String) map.get("employeePosition"));
        employees.setTenantId((Long.parseLong(map.get("tenantId") + "")));
        employees.setAuthority(1);
        employeeService.addTenant(employees);
        HashMap res = new HashMap();
        res.put("msg", "添加成功");
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //删除租户
    @RequestMapping(value = "/deleteTenant", method = RequestMethod.POST)
    public ResponseEntity deleteTenant(@RequestBody Map map) {
        Long employeeId = Long.parseLong((String) map.get("employeeId"));
        employeeService.deleteTenant(employeeId);
        HashMap res = new HashMap();
        res.put("msg", "删除成功");
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //根据id查询租户
    @RequestMapping(value = "/queryTenantById", method = RequestMethod.POST)
    public ResponseEntity queryTenantById(@RequestBody Map map) {
        HashMap res = new HashMap();
        Employees employee = employeeService.queryTenantById(Long.parseLong((String) map.get("employeeId")));
        res.put("employee", employee);
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //更新租户信息
    @RequestMapping(value = "/updateTenant", method = RequestMethod.POST)
    public ResponseEntity updateTenant(@RequestBody Map map) {
        Employees employees = employeeService.queryTenantById(Long.parseLong((String) map.get("employeeId")));
        employees.setEmployeePassword((String) map.get("employeePassword"));
        employees.setEmployeeName((String) map.get("employeeName"));
        employees.setEmployeeGender((String) map.get("employeeGender"));
        employees.setEmployeeCompany((String) map.get("employeeCompany"));
        employees.setEmployeeEmail((String) map.get("employeeEmail"));
        employees.setEmployeeAddress((String) map.get("employeeAddress"));
        employees.setEmployeePosition((String) map.get("employeePosition"));
        employeeService.updateTenant(employees);
        HashMap res = new HashMap();
        res.put("msg", "更新成功");
        return new ResponseEntity(res, HttpStatus.OK);
    }

}
